/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.uloha3.swing;

import cz.muni.fi.pv168.uloha3.swing.workers.OwnershipAddSwingWorker;
import cz.muni.fi.pv168.uloha3.entities.Owner;
import cz.muni.fi.pv168.uloha3.entities.RealEstate;
import java.awt.Color;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author morty
 */
public class SwingAddOwnerships extends javax.swing.JFrame {
    
    private final Owner excludeForOwner;
    private final RealEstate excludeForRealEstate;
    
    /**
     * Creates new form SwingAddOwnerships
     */
    public SwingAddOwnerships(Owner excludeForOwner) {
        this.excludeForOwner = excludeForOwner;
        this.excludeForRealEstate = null;
        initComponents();
    }
    
    public SwingAddOwnerships(RealEstate excludeForRealEstate) {
        this.excludeForOwner = null;
        this.excludeForRealEstate = excludeForRealEstate;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonAddOwnerships = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableOwnerships = new javax.swing.JTable();
        jTextFieldInvestment = new javax.swing.JTextField();
        jLabelInvestment = new javax.swing.JLabel();
        jLabelInvestmentErr = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButtonAddOwnerships.setText("Add");
        jButtonAddOwnerships.setEnabled(false);
        jButtonAddOwnerships.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddOwnershipsActionPerformed(evt);
            }
        });

        jTableOwnerships.setModel(excludeForOwner == null ? new OwnersTableModel(excludeForRealEstate) : new EstatesTableModel(excludeForOwner));
        jTableOwnerships.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        jTableOwnerships.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableOwnershipsMouseClicked(evt);
            }
        });
        jTableOwnerships.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTableOwnershipsFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTableOwnershipsFocusLost(evt);
            }
        });
        jScrollPane2.setViewportView(jTableOwnerships);

        jTextFieldInvestment.setText("0");

        jLabelInvestment.setText("Investment");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelInvestment)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelInvestmentErr)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jTextFieldInvestment, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonAddOwnerships, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAddOwnerships)
                    .addComponent(jTextFieldInvestment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelInvestment)
                    .addComponent(jLabelInvestmentErr))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAddOwnershipsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddOwnershipsActionPerformed
        if(jTableOwnerships.getSelectedRowCount() == 0) return;
        if(jTextFieldInvestment.getText().isEmpty()) {
            jLabelInvestmentErr.setForeground(Color.red);
            jLabelInvestmentErr.setText("!");
            return;
        } else {
            jLabelInvestmentErr.setText("");
        }
        
        BigDecimal investment;
        try {
            investment = new BigDecimal(jTextFieldInvestment.getText());
        } catch(NumberFormatException ex) {
            jLabelInvestmentErr.setForeground(Color.red);
            jLabelInvestmentErr.setText("!");
            return;
        }
        
        if(investment.compareTo(BigDecimal.ZERO) != 1) {
            jLabelInvestmentErr.setForeground(Color.red);
            jLabelInvestmentErr.setText("!");
            return;
        }
        
        if(null != excludeForOwner) {
            List<RealEstate> newRealEstates = new ArrayList<RealEstate>();
            for(int rownum : jTableOwnerships.getSelectedRows()) {
                newRealEstates.add(((EstatesTableModel)jTableOwnerships.getModel()).getEstateAt(rownum));
            }
            OwnershipAddSwingWorker worker = new OwnershipAddSwingWorker(excludeForOwner, newRealEstates, investment);
            worker.execute();
        } else {
            List<Owner> newOwners = new ArrayList<Owner>();
            for(int rownum : jTableOwnerships.getSelectedRows()) {
                newOwners.add(((OwnersTableModel)jTableOwnerships.getModel()).getOwnerAt(rownum));
            }
            OwnershipAddSwingWorker worker = new OwnershipAddSwingWorker(excludeForRealEstate, newOwners, investment);
            worker.execute();
        }
        this.dispose();
    }//GEN-LAST:event_jButtonAddOwnershipsActionPerformed

    private void jTableOwnershipsFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTableOwnershipsFocusGained
    }//GEN-LAST:event_jTableOwnershipsFocusGained

    private void jTableOwnershipsFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTableOwnershipsFocusLost
        if(jTableOwnerships.getSelectedRowCount() == 0) jButtonAddOwnerships.setEnabled(false);
    }//GEN-LAST:event_jTableOwnershipsFocusLost

    private void jTableOwnershipsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableOwnershipsMouseClicked
        this.jButtonAddOwnerships.setEnabled(true);
    }//GEN-LAST:event_jTableOwnershipsMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAddOwnerships;
    private javax.swing.JLabel jLabelInvestment;
    private javax.swing.JLabel jLabelInvestmentErr;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableOwnerships;
    private javax.swing.JTextField jTextFieldInvestment;
    // End of variables declaration//GEN-END:variables
}
